<?php
/*
e107Shop Gen 2 rev 1 - Daniel Hewes - http://www.myTipper.com
*/
/******************************************************************************
* Admin Index Page
*****************************************************************************/
require("../../../class2.php");
require("../library/admin/adminapi.php");

if(!getperms("P")){header("location:".e_BASE."index.php"); exit; }
require_once(e_ADMIN."auth.php");

//
// Declare required Class variables as members of the calls
//
$sql = new db;

// obtain webstore-name from db
$sql->db_Select($e107shop_db_name['settings'], "store_name", "");
while($row=$sql->db_Fetch()) {
    $store_name = $row["store_name"];
}

//
// Wrap the entire admin front panel in a table
//

$e107shop_text ="<br /><h2 style='text-align: center;'>$store_name</h2>
                <table border='0' width='95%' cellpadding='3'>";

$e107shop_text .= "    <tr>
                <td valign='top' align='left' width='45%'>
                    <!-- Table within the cell for stats/info display -->
                    <center>
                    <table class='fborder' width='90%'>
                        <tr>
                            <td class='fcaption' colspan='2' width='100%' style='text-align: center; font-weight: bold;'>
                                ".lan_admin_info_block_title."
                            </td>
                        </tr>";

// We need to figure out if there is at least an active category, 
// otherwise the shop won't display something anyway.
// TODO: write this info also to screen when the user enters the shop
//       we need to let the user know the shop is alive, but there is nothing to display
//       it's also possible a customer only wants products, no categories.
//       maybe we should add at least one record in the category table when it is created ?
//       and add a switch to the settings screen where the user can choose products only ?
//       we need to think about that for the next release.
//       for now at least force one category.
$Category_count = $sql->db_Count($e107shop_db_name['categories'], "(*)", "WHERE active=1"); 

################## Count & Display ACTIVE Categories in the database #######################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    ".lan_admin_info_block_active_category."
                </td>
                <td class='forumheader2'>";

if(!$Category_count) {
    $e107shop_text .= "<a href='admin_manage_categories.php'>".lan_admin_info_no_category."</a>";
} else {
    $e107shop_text .= $sql->db_Count($e107shop_db_name['categories'], "(*)", "WHERE active=1") or die(mysql_error());
}

$e107shop_text .="</td>
            </tr>";

###################### End ACTIVE Category Count & Display #################################

################## Count & Display INACTIVE categories in the database #######################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    ".lan_admin_info_block_inactive_category."
                </td>
                <td class='forumheader2'>";

$e107shop_text .= $sql->db_Count($e107shop_db_name['categories'], "(*)", "WHERE active<>1");



$e107shop_text .="</td>
            </tr>";

###################### End INACTIVE category Count & Display #################################

################## Count & Display products in the database #######################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    ".lan_admin_info_block_product."
                </td>
                <td class='forumheader2'>";

// Same goes for products, if there are none that's bad
$Product_count = $sql->db_Count($e107shop_db_name['products'], "(*)", "WHERE active=1");

// for now we can fix this hardcoded. Changed product_management.php to make it work :-)
// TODO: French language needs to be translated for lan_no_category and lan_no_products on line 105
if(!$Product_count){
    $e107shop_text .= "<a href='admin_manage_products.php'>".lan_admin_info_no_products."</a>";
} else {
    $e107shop_text .= $Product_count;
}

$e107shop_text .="</td>
            </tr>";

###################### End Product Count & Display #################################


################## Count & Display INACTIVE products in the database #######################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    ".lan_admin_info_block_inactive_product."
                </td>
                <td class='forumheader2'>";

$e107shop_text .= $sql->db_Count($e107shop_db_name['products'], "(*)", "WHERE active=2");



$e107shop_text .="</td>
            </tr>";

###################### End INACTIVE Product Count & Display #################################

$e107shop_text .="</td></tr></table>";

$e107shop_text .= "<td valign='top' align='left' width='5%'>&nbsp;</td>
                <td valign='top' align='left' width='45%'>
                    <!-- Table within the cell for orders display -->
                    <center>
                    <table class='fborder' width='90%'>
                        <tr>
                            <td class='fcaption' colspan='2' width='100%' style='text-align: center; font-weight: bold;'>
                                ".lan_admin_orders_block_title."
                            </td>
                        </tr>";
####################### Show orders waiting for approval ####################################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    <a href='admin_manage_orders.php?o=1'>".lan_admin_orders_pending_orders."</a>
                </td>
                <td class='forumheader2'>";
                $sql2 = new db;
                $i = 0;
                $sql2->db_Select($e107shop_db_name['orders'], "*", "status = 'pending' Group by order_number");
                while($row2=$sql2->db_Fetch())
                {
                    $i++;
                }

$e107shop_text .= $i;
$e107shop_text .="</td>
            </tr>";
#############################################################################################

##################### Orders block -> Total complete orders ########################################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    <a href='admin_manage_orders.php?o=2'>".lan_admin_orders_paid_orders."</a>
                </td>
                <td class='forumheader2'>";
                
                $sql2 = new db;
                $i = 0;
                $sql2->db_Select($e107shop_db_name['orders'], "*", "status = 'paid' Group by order_number");
                while($row2=$sql2->db_Fetch())
                {
                    $i++;
                }


$e107shop_text .=$i."</td>
            </tr>";

###############################################################################################

##################### Orders block -> Total offline orders ########################################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    <a href='admin_manage_orders.php?o=3'>".lan_admin_orders_complete_orders."</a>
                </td>
                <td class='forumheader2'>";
                $sql2 = new db;
                $i = 0;
                $sql2->db_Select($e107shop_db_name['orders'], "*", "status = 'complete' Group by order_number");
                while($row2=$sql2->db_Fetch())
                {
                    $i++;
                }

$e107shop_text .=$i."</td>
            </tr>";




##################### Orders block -> Total orders ########################################

$e107shop_text .= "<tr>
                <td class='forumheader'>
                    ".lan_admin_orders_block_total_orders."
                </td>
                <td class='forumheader2'>";
                $sql2 = new db;
                $i = 0;
                $sql2->db_Select($e107shop_db_name['orders'], "*", "lineID>0 Group by order_number");
                while($row2=$sql2->db_Fetch())
                {
                    $i++;
                }

$e107shop_text .=$i."</td>
            </tr>";

$e107shop_text .="</table>";

#############################################################################################




//
// Close  the tables
//

$e107shop_text .="</td></tr></table>";

$e107shop_text.="<center><div style='width: 90%; text-align: left;'><br />";

// TODO: French translation of the license text... around line 176
$e107shop_text.= lan_admin_license_text;

$e107shop_text.="</div></center>";

// TODO: French translation of the admin-title... round line 154
//$ns->tablerender("<center>".lan_admin_menu_index."</center>", $e107shop_text);
$ns->tablerender("<center>".lan_admin_menu_index."</center>", $e107shop_text);
require_once(e_ADMIN."footer.php");
